/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ajustes.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jose Carlos Palma
 */
public class EmpresaPermiso {

    private Usuario user;
    private Empresa empresa;
    private List<DivisionPermiso> divisiones;

    public EmpresaPermiso(Usuario user, int empresaId, String empresaNombre) {
        this.user = user;
        this.empresa = new Empresa(empresaId, empresaNombre);
        this.divisiones = new ArrayList<DivisionPermiso>();
    }

    public void cargarDivisiones(Connection cnx) {

        StringBuilder sb = new StringBuilder();
        sb.append("SELECT PLUDIVISION, NOMBRE");
        sb.append("  FROM DIVISIONES A");
        sb.append(" WHERE EXISTS (SELECT 1");
        sb.append("                 FROM DIVAUTORIZACIONES B");
        sb.append("                WHERE A.PLUDIVISION = B.DIVISIONID");
        sb.append("                  AND B.USUARIOID = ?");
        sb.append("                  AND B.OPCIONID IN (318,312,321)");
        sb.append("                  AND B.EMPRESAID = ?)");
        sb.append("    OR (? = 1 AND PLUEMPRESA = ?)");
        try {
            PreparedStatement stmt = cnx.prepareStatement(sb.toString());
            stmt.setInt(1, getUser().getId());
            stmt.setInt(2, getEmpresa().getId());
            stmt.setInt(3, getUser().getId());
            stmt.setInt(4, getEmpresa().getId());
            ResultSet rs = stmt.executeQuery();
            
            //Agregar las divisiones
            while (rs.next()) {
                Division division = new Division(rs.getInt(1), rs.getString(2), getEmpresa().getId());
                DivisionPermiso dp = new DivisionPermiso(division, "", "", "");
                getDivisiones().add(dp);
            }

        } catch (SQLException ex) {
            Logger.getLogger(EmpresaPermiso.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * @return the user
     */
    public Usuario getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(Usuario user) {
        this.user = user;
    }

    /**
     * @return the empresa
     */
    public Empresa getEmpresa() {
        return empresa;
    }

    /**
     * @param empresa the empresa to set
     */
    public void setEmpresa(Empresa empresa) {
        this.empresa = empresa;
    }

    /**
     * @return the divisiones
     */
    public List<DivisionPermiso> getDivisiones() {
        return divisiones;
    }
    
    
    public DivisionPermiso getDivision(int divisionId){
        
        for( DivisionPermiso dp : divisiones ){
            if( dp.getDivision().getId() == divisionId )
                return dp;
        }
        return null;
        
    }
    
    public DivisionPermiso getDivision(DivisionPermiso division){
        return getDivision(division.getDivision().getId());
    }

    /**
     * @param divisiones the divisiones to set
     */
    public void setDivisiones(List<DivisionPermiso> divisiones) {
        this.divisiones = divisiones;
    }

    @Override
    public String toString() {
        return getEmpresa().toString();
    }
    
    
    @Override
    public boolean equals(Object obj) {
        
        //por referencia (Reflexivo)
        if (this == obj)
            return true;
        
        //por objeto nulo (No Nulo)
        if (obj == null)
            return false;
        
        //
        if ( !(obj instanceof EmpresaPermiso))
            return false;
        
        
        EmpresaPermiso tmp = (EmpresaPermiso)obj;
        return getEmpresa().equals(tmp.getEmpresa());
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + (this.user != null ? this.user.hashCode() : 0);
        hash = 67 * hash + (this.empresa != null ? this.empresa.hashCode() : 0);
        return hash;
    }

    
}
